


# 创建一个名为 x 的变量，并赋值为 5
x = 5
# 创建一个名为 name 的变量，并赋值为 "Alice"
name = "Alice"





# 定义一个整数变量
age = 25
# 定义一个字符串变量
greeting = "Hello, World!"
# 定义一个浮点数变量
height = 1.75


x


type(x)


type(greeting)





# 有效的变量名
my_variable = 10
name2 = "Bob"
_user_id = 123

# 无效的变量名
2nd_variable = 20  # 错误：不能以数字开头
my-variable = 30   # 错误：不能包含连字符
for = 40           # 错误：不能使用保留关键字





# 算术运算
x = 5
y = 3
z = x + y  # z 的值为 8

# 字符串拼接
first_name = "Alice"
last_name = "Smith"
full_name = first_name + " " + last_name  # full_name 的值为 "Alice Smith"

# 逻辑运算
is_adult = age >= 18  # age 来自之前的定义，值为 25，结果为 True

# 打印结果
print(z)           # 输出: 8
print(full_name)   # 输出: Alice Smith
print(is_adult)    # 输出: True





age = 25
temperature = -5
print(type(age))  # 输出: <class 'int'>


type(temperature)





height = 1.75
pi = 3.14159
print(type(height))  # 输出: <class 'float'>


pi





name = "Alice"
greeting = 'Hello, World!'
print(type(name))  # 输出: <class 'str'>
# 字符串拼接
message = "Hello, " + name  # 输出: Hello, Alice
# 字符串长度
length = len(name)  # 输出: 5
# 字符串切片
first_two = name[:3]  # 输出: Al


message1=greeting+name


first_two





is_student = False
has_car = False
print(type(is_student))  # 输出: <class 'bool'>


# 逻辑运算
can_vote = age >= 18 and is_student  # 输出: True
print(can_vote)





# 创建一个包含字符串的列表
fruits = ["apple", "banana", "cherry"]
# 创建一个包含整数的列表
numbers = [1, 2, 3, 4, 5]
# 创建一个混合类型的列表
mixed = [1, "hello", 3.14, True]





fruits = ["apple", "banana", "cherry"]
print(fruits[0])   # 输出: apple
print(fruits[1])   # 输出: banana
print(fruits[-1])  # 输出: cherry
print(fruits[-2]) 





fruits = ["apple", "banana", "cherry"]

# 添加元素到末尾
fruits.append("orange")
print(fruits)  # 输出: ["apple", "banana", "cherry", "orange"]

# 在索引 1 处插入元素
fruits.insert(1, "blueberry")
print(fruits)  # 输出: ["apple", "blueberry", "banana", "cherry", "orange"]

# 删除指定值的元素
fruits.remove("banana")
print(fruits)  # 输出: ["apple", "blueberry", "cherry", "orange"]

# 删除并返回最后一个元素
last_fruit = fruits.pop()
print(last_fruit)  # 输出: orange
print(fruits)  # 输出: ["apple", "blueberry", "cherry"]

# 对数字列表进行排序
numbers = [3, 1, 4, 2]
numbers.sort()
print(numbers)  # 输出: [1, 2, 3, 4]

# 反转列表
numbers.reverse()
print(numbers)  # 输出: [4, 3, 2, 1]





numbers = [0, 1, 2, 3, 4, 5]
print(numbers[1:4])  # 输出: [1, 2, 3]
print(numbers[:3])   # 输出: [0, 1, 2]
print(numbers[3:])   # 输出: [3, 4, 5]
print(numbers[-2:])  # 输出: [4, 5]





# 创建一个元组
coordinates = (3, 4)
# 创建一个单元素元组（注意逗号）
single = (5,)


single





coordinates = (3, 4)
print(coordinates[0])  # 输出: 3
print(coordinates[1])  # 输出: 4





coordinates = (3, 4)
# coordinates[0] = 5  # TypeError: 'tuple' object does not support item assignment





def get_point():
    return (3, 4)

point = get_point()
print(point)  # 输出: (3, 4)





# 创建一个字典
student = {"name": "Alice", "age": 20, "major": "Computer Science"}





print(student["name"])  # 输出: Alice
print(student["age"])   # 输出: 20





student = {"name": "Alice", "age": 20}
student["age"] = 21        # 修改现有键的值
student["grade"] = "A"     # 添加新键值对
print(student)  # 输出: {"name": "Alice", "age": 21, "grade": "A"}

del student["age"]  # 删除键 "age"
print(student)  # 输出: {"name": "Alice", "grade": "A"}

print(student.keys())    # 输出: dict_keys(['name', 'grade'])
print(student.values())  # 输出: dict_values(['Alice', 'A'])
print(student.items())   # 输出: dict_items([('name', 'Alice'), ('grade', 'A')])





# 统计列表中元素的出现次数
numbers = [1, 2, 2, 3, 3, 3]
count = {}
for num in numbers:
    if num in count:
        count[num] += 1
    else:
        count[num] = 1
print(count)  # 输出: {1: 1, 2: 2, 3: 3}





# 创建一个集合
fruits = {"apple", "banana", "cherry", "apple"}
print(fruits)  # 输出: {"apple", "banana", "cherry"}（自动去重）





set1 = {1, 2, 3}
set2 = {3, 4, 5}

# 并集
print(set1 | set2)  # 输出: {1, 2, 3, 4, 5}

# 交集
print(set1 & set2)  # 输出: {3}

# 差集
print(set1 - set2)  # 输出: {1, 2}

# 添加元素
set1.add(4)
print(set1)  # 输出: {1, 2, 3, 4}

# 删除元素
set1.remove(2)
print(set1)  # 输出: {1, 3, 4}





# 去除列表中的重复项
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers)
print(unique_numbers)  # 输出: {1, 2, 3, 4, 5}

# 成员测试
if 3 in unique_numbers:
    print("3 is in the set")  # 输出: 3 is in the set






